<template>
  <div class="login-wrapper">
    <div class="left-banner">
      <div class="box">
        <img src="../assets/image/login_background.png" class="img-block">
        <div class="banner"></div>
      </div>
    </div>
    <div class="login-pos">
      <div class="login-container">
        <div class="title-container">
          <img src="../assets/logo/logo_color.png" class="logo">
          <div class="title">5G+管理平台</div>
        </div>
        <div class="item-container t-form">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" size="medium">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-form-item prop="code">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode"/>
              </div>
            </el-form-item>

            <div class="check-container remember-pwd">
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
<!--              <router-link :to="'/forgetPwd'" class="tip">-->
<!--                <span style="color: #333">忘记密码？</span>-->
<!--              </router-link>-->
            </div>
            <el-form-item style="width:100%;">
              <el-button
                class="login-btn"
                :loading="loading"
                size="medium"

                style="width:100%;height: 40px;"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>

          </el-form>
          <div class="form_foot" style="float: right;margin-right:4px;margin-bottom: 5px;">
            <el-tooltip class="item" effect="light" placement="top">
              <div slot="content" style="width: 200px;height:200px;display: flex;justify-content: center;align-items: center;flex-direction: column;">
                <div>
                  <h3 style="padding: 0;margin: 0px 0 0;">扫描二维码下载app</h3>
                </div>
              </div>
              <router-link class="link-type" to="" style="color: #1A55CB;font-size:14px;">APP下载</router-link>
            </el-tooltip>
<!--            &nbsp;&nbsp; <span style="color: #1A55CB;">|</span> &nbsp;&nbsp;-->
<!--            <router-link class="link-type" :to="'/forgetPwd'" style="color: #1A55CB;font-size:14px;">找回密码-->
<!--            </router-link>-->
          </div>
        </div>
      </div>
      <!--      <footer class="copyright">{{ footer }}</footer>-->
    </div>
  </div>
</template>

<script>
import defaultSettings from "@/settings.js";
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from "@/utils/jsencrypt";
import $ from "jquery";
import regexp from '@/api/regexp/regexp'

export default {
  name: "Login",
  /*components: {
    vueQr
  },*/
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      title: defaultSettings.title,
      //footer: defaultSettings.footer,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "用户名不能为空"}
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"},
          {required: true, validator: regexp.validatePassword, trigger: "blur"}
        ],
        code: [{required: true, trigger: "change", message: "验证码不能为空"}]
      },
      loading: false,
      redirect: undefined,
      appDownloadUrl: ''
    };
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    showPassword() {
      $(".login-type-title-password")
        // .css("color", "#1890ff")
        .css("color", "#1A55CB")
        .css("border-bottom", "2px solid #1890ff");
      $(".login-type-title-qrcode")
        .css("color", "#000000")
        .css("border-bottom", "none");
      $("#qrcodeType").hide();
      $("#passwordType").show();
    },
    showQrcode() {
      $(".login-type-title-password")
        .css("color", "#000000")
        .css("border-bottom", "none");
      $(".login-type-title-qrcode")
        // .css("color", "#1890ff")
        .css("color", "#1A55CB")
        .css("border-bottom", "2px solid #1890ff");
      $("#qrcodeType").show();
      $("#passwordType").hide();
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      }).catch((e) => {
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({path: this.redirect || "/index"})
            })
            .catch((e) => {
              console.log(e)
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
    gotoForgetPwd() {
      // console.log("test");
      this.$router.push({name: "ForgetPwd", params: {}});
    },
    gotoSignUp() {
      this.$router.push({path: "/signUp"});
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login-wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-size: cover;
  background-position: 100%;
  position: relative;
  background-color: #fff;

  &:after {
    position: absolute;
    top: 0;
    left: 0;
    content: "";
    background-color: #000;
    opacity: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
  }


  .login-container {
    position: relative;
    width: 430px;
    height:440px;
    background: #fff;
    border-radius: 15px;
    line-height: 22px;

    .title-container {
      position: relative;
      width: 100%;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;

      .logo {
        width: 40px;
      }

      .title {
        font-size: 28px;
        line-height: 44px;
        color: rgba(0, 0, 0, 0.9);
        margin-top: 4px;
        text-align: left;
        margin-left:8px;
        .margin-no {
          margin-top: 0;
        }
      }
    }

    .welcome {
      width: 100%;
      text-align: center;
      color: rgb(157, 157, 157);
      letter-spacing: 1px;
      font-size: 16px;
    }

    .item-container {
      position: relative;
      width: 100%;
      // padding: 20px 40px;
      padding: 0px 40px;
      margin-top: 10px;

      .check-container {
        display: flex;
        align-items: center;
      }

      .remember-pwd {
        margin-bottom: 16px;
        justify-content: space-between;
        align-items: center;
      }

      .tip {
        //color: #0052d9;
        color: #1A55CB;
      }
    }

    .t-form {
      font-size: 14px;
    }
  }
}

.copyright {
  font-size: 14px;
  position: absolute;
  width: 100%;
  text-align: center;
  // left: 5%;
  bottom: 64px;
  color: #fff;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-pos {
  z-index: 99;
  width: 600px;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  //background: rgb(48, 105, 211);
  background: #1A55CB;
  display: flex;
  justify-content: center;
  align-items: center;
}

.left-banner {
  z-index: 99;
  width: calc(100% - 600px);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  background: #eff1f3;
  display: flex;
  justify-content: center;
  align-items: center;

  .box {
    position: relative;
    width: 70%;

    .img-block {
      position: relative;
      z-index: 9;
      width: 100%;
    }

    .banner {
      position: absolute;
      z-index: 99;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-image: url("../assets/image/login_background.png");
      background-size: 100% 100%;
    }
  }

}

.login-btn {
  //background: rgb(51, 92, 246);
  background-color: #1A55CB;
  color: #fff;

  &:hover{
    //background: rgb(50, 91, 246);
    background-color: #1A55CB;
    color: #fff;
  }
  &:focus{
    background-color: #1A55CB;
    border-color: #1A55CB;
    color: #fff;
  }
}
</style>
